package com.hanj.myserver.JD;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.hanj.myserver.entity.JdUser;
import com.hanj.myserver.utils.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * The type Jd controller.
 *
 * @author hanj.cn @outlook.com
 */
@RestController
@RequestMapping("/jd")
public class JdController {

    // 通过校验的用户数量
    private int userCount;
    // 上一次提交反馈是否结束
    private boolean isFeedbackComplete = true;
    // 上一次提交是否结束
    private boolean isSubmitComplete = true;

    /**
     * 获取代挂页面上传的数据
     *
     * @param user the user
     * @return response jd data
     */
    @PostMapping("/submitData")
    public Result<String> getJdData(JdUser user) {

        String time = MyUtil.getDate();

        if (!isSubmitComplete) {
            return Result.failed("服务器繁忙,请稍后再试");
        }

        isSubmitComplete = false;

        // 判空
        if (StrUtil.isBlank(user.getNickname()) || StrUtil.isBlank(user.getWxPusherId()) || StrUtil.isBlank(user.getCookie())) {
            isSubmitComplete = true;
            return Result.failed("存在必填项未填,请检查");
        }

        // 判断数据是否为不定长的空格
        if (MyUtil.strIsEmpty(user.getNickname(), user.getCookie(), user.getWxPusherId())) {
            isSubmitComplete = true;
            return Result.failed("不允许必填项为空格,请检查");
        }

        // 判断cookie格式
        if (!(user.getCookie().contains("pt_key") && user.getCookie().contains("pt_pin"))) {
            isSubmitComplete = true;
            return Result.failed("cookie格式错误,请检查");
        }

        // 判断uid格式
        if (!user.getWxPusherId().startsWith("UID_")) {
            isSubmitComplete = true;
            return Result.failed("UID格式错误,请检查");
        }

        // 格式检查完毕，发送消息
        SendMsg.byBark("提交代挂", URLUtil.encode("[" + user.getNickname() + "]" + "提交了代挂"), URLUtil.encode(user.getCookie() + "\n" + user.getWxPusherId() + "\n" + time));

        // 需要被写入的内容
        String contentToFile = "序号:" + (++userCount) + "\n" + user + "\n" + time + "\n\n";

        // 写入到文件
        MyUtil.writeToFile(Constant.COOKIE_FILEPATH, contentToFile, true);

        // 可以加入到青龙，获取响应状态码判断是否成功提交
        if (MyUtil.judgeCodeAndSendMsg(user)) {
            // 赋值
            isSubmitComplete = true;
            return Result.successWithoutData("代挂成功\n请仔细阅读公众号的推送内容并关注之后的资产推送和ck失效推送\n如果账号失效请回到本页面重新获取cookie然后提交即可\n如有疑问请及时在下方提交");

        } else {
            isSubmitComplete = true;
            return Result.failed("代挂失败,请在下方提交反馈");
        }

    }

    @PostMapping("/feedback")
    public Result<String> feedback(String nickname, String content, String email) {

        String time = MyUtil.getDate();

        if (!isFeedbackComplete) {
            return Result.failed("服务器繁忙,请稍后再试");
        }

        isFeedbackComplete = false;

        // 判空
        if (StrUtil.isBlank(nickname) || StrUtil.isBlank(content) || StrUtil.isBlank(email)) {
            isFeedbackComplete = true;
            return Result.failed("存在必填项未填,请检查");
        }

        // 发送消息
        SendMsg.byBark("代挂反馈", URLUtil.encode("[" + nickname + "]" + "提交了反馈"), URLUtil.encode(content + "\n" + email + "\n" + time));

        // 需要被写入的内容
        String contents = "[" + nickname + "]" + "\n" + content + "\n" + email + "\n" + time + "\n\n";

        // 写入文件
        MyUtil.writeToFile(Constant.FEEDBACK_FILEPATH, contents, true);

        isFeedbackComplete = true;
        return Result.successWithoutData("提交成功\n回复将会以邮件的形式稍晚些发送,请注意查收");

    }

    @PostMapping("/refreshConfig")
    public Result<String> refreshConfig(String command, String password) {

        if (!Objects.equals(password, Initialization.getAdminPassword())) {
            return Result.failed(Result.FAILED);
        }

        switch (command) {
            case "refresh": {
                Initialization.reloadSetting();
                return Result.successWithoutData(Result.SUCCESS);
            }
            case "test": {

                return Result.successWithoutData(Initialization.getTestRefresh());
            }
            default: {
                return Result.successWithoutData(Result.FAILED);
            }
        }
    }

    @GetMapping("/announcement")
    public Result<String> announcement() {

        return Result.successWithData("成功获取公告", Initialization.getAnnouncement());
    }

}
